package com.wlstock.funi.utils;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.wlstock.funi.constant.ServiceConstants;
import com.wlstock.funi.service.TimerService;

/**
 * Created by coder80 on 2014/10/31.
 */

public class ServiceUtil {

	public static boolean isServiceRunning(Context context, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceInfos = activityManager.getRunningServices(ServiceConstants.RETRIVE_SERVICE_COUNT);

		if (null == serviceInfos || serviceInfos.size() < 1) {
			return false;
		}

		for (int i = 0; i < serviceInfos.size(); i++) {
			if (serviceInfos.get(i).service.getClassName().contains(className)) {
				isRunning = true;
				break;
			}
		}
		Log.i("ServiceUtil-AlarmManager", className + " isRunning =  " + isRunning);
		return isRunning;
	}

	public static void invokeTrackService(Context context) {
		Log.i("ServiceUtil-AlarmManager", "invokeTimerPOIService wac called..");
		PendingIntent alarmSender = null;
		Intent startIntent = new Intent(context, TimerService.class);
		startIntent.setAction(ServiceConstants.TimerService_ACTION_START);
		try {
			alarmSender = PendingIntent.getService(context, 0, startIntent,
					PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Exception e) {
			Log.i("ServiceUtil-AlarmManager", "failed to start " + e.toString());
		}
		AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
		am.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), ServiceConstants.ELAPSED_TIME_UPDATE_OPERATE,alarmSender);
	}

	public static void cancelTrackService(Context context) {
		Log.i("ServiceUtil-AlarmManager", "cancleAlarmManager to start ");
		Intent intent = new Intent(context, TimerService.class);
		intent.setAction(ServiceConstants.TimerService_ACTION_START);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager alarm = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
		alarm.cancel(pendingIntent);
	}
	
	public static <T> void invokeService(Context context, Class<T> service, String action) {
		Log.i("ServiceUtil-AlarmManager", "invokeTimerPOIService wac called..");
		invokeService(context, service, action, ServiceConstants.ELAPSED_TIME_UPDATE_OPERATE);
	}
	
	public static <T> void invokeService(Context context, Class<T> service, String action,int elapsedTime ) {
		Log.i("ServiceUtil-AlarmManager", "invokeTimerPOIService wac called..");
		PendingIntent alarmSender = null;
		Intent startIntent = new Intent(context, service);
		startIntent.setAction(action);
		try {
			alarmSender = PendingIntent.getService(context, 0, startIntent,PendingIntent.FLAG_UPDATE_CURRENT);
		} catch (Exception e) {
			Log.i("ServiceUtil-AlarmManager", "failed to start " + e.toString());
		}
		AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
		am.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), elapsedTime ,alarmSender);
	}
	
	public static <T> void cancelService(Context context,Class<T> service,String action) {
		Log.i("ServiceUtil-AlarmManager", "cancleAlarmManager to start ");
		Intent intent = new Intent(context, service);
		intent.setAction(action);
		PendingIntent pendingIntent = PendingIntent.getService(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
		AlarmManager alarm = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
		alarm.cancel(pendingIntent);
	}
	
	
}
